package moodish.comm.component;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.UnknownHostException;

import moodish.comm.ServerSideMessage;
import moodish.comm.ClientSideMessage;

import org.junit.Test;

public class AllTests {

	
	
	@Test	
	public  void test_connect() throws UnknownHostException, IOException, InterruptedException {
	
		/**
		 * Create two instances of the ClientCommComponent. 
		 */	
		
		ClientCommComponent clientTest1 = new ClientCommComponent();	
		ClientCommComponent clientTest2 = new ClientCommComponent();
		
		//testClientCommComponent.disconnect();	
		
		
		/**
		 * Create an instance of the ServerCommComponent. 
		 */	
		
		ServerCommComponent serverTest = new ServerCommComponent();		
		

		
		/**
		 * Test the several public methods. 
		 */	
		serverTest.start();

		clientTest1.connect("localhost", "Miguel");
		clientTest2.connect("localhost", "Luis");
		
		assertEquals(Boolean.TRUE,serverTest.clientIsConnected("Miguel"));			
				
		System.out.println("Server: Miguel is connected?"+serverTest.clientIsConnected("Miguel"));
		
				System.out.println("Server: Miguel is connected?"+serverTest.clientIsConnected("Miguel"));
		
				assertEquals(Boolean.TRUE,(serverTest.clientIsConnected("Miguel")));
		
				System.out.println("Client: Miguel is connected?"+clientTest1.isConnected());
					
		
		
			System.out.println("Server: Luis is connected?"+serverTest.clientIsConnected("Luis"));
		
			System.out.println("Server: Luis is connected?"+serverTest.clientIsConnected("Luis"));
		
			System.out.println("Server: Luis is connected?"+serverTest.clientIsConnected("Luis"));
		
		
		
		
		clientTest1.friendship("Ricardo");
		
		clientTest1.friendship("Luis");
		
		clientTest1.unfriendship("Ricardo");
		
		clientTest1.sendMoodishMessage("hello world");

		assertEquals(Boolean.TRUE,serverTest.hasNextMessage());
		System.out.println(serverTest.hasNextMessage()+ " a");
		assertEquals(Boolean.TRUE,serverTest.hasNextMessage());
		System.out.println(serverTest.hasNextMessage());
		assertEquals(Boolean.TRUE,serverTest.hasNextMessage());
		System.out.println(serverTest.hasNextMessage());
		assertEquals("Miguel",serverTest.getNextMessage().getClientNickname());
//		System.out.println(serverTest.getNextMessage().getClientNickname());
		assertEquals("Luis",serverTest.getNextMessage().getClientNickname());
//		System.out.println(serverTest.getNextMessage().getClientNickname());
		assertEquals("Ricardo",serverTest.getNextMessage().getClientNickname());
//		System.out.println(serverTest.getNextMessage().getClientNickname());
		assertEquals("Luis",serverTest.getNextMessage().getClientNickname());
//		System.out.println(serverTest.getNextMessage().getClientNickname());
		assertEquals("Ricardo",serverTest.getNextMessage().getClientNickname());
//		System.out.println(serverTest.getNextMessage().getClientNickname());
		
		ServerSideMessage msg = serverTest.getNextMessage();
		assertEquals("hello world",msg.getPayload());
		System.out.println(msg.getPayload());
		
		
		
		serverTest.sendMoodishMessage("Miguel", "Luis", msg.getPayload()+" Miguel to Luis");
		
		serverTest.sendMoodishMessage("Luis", "Miguel", "Luis to Miguel");
		
		System.out.println(clientTest1.hasNextMessage() + "    AQUI");
		
		System.out.println(clientTest1.hasNextMessage() + "    AQUI");
		
		System.out.println(clientTest1.hasNextMessage() + "    Tem nova mensagem?");
		
		
		
		ClientSideMessage msg2 = null; 
		msg2 = clientTest1.getNextMessage();
		
		System.out.println(msg2.getPayload());
		System.out.println(msg2.getSendersNickname());

		ClientSideMessage msg3 = clientTest2.getNextMessage();
		
		System.out.println(msg3.getPayload());
		
	
		
		serverTest.disconnectClient("Ricardo");
		
		serverTest.disconnectClient("Luis");
		
		clientTest1.disconnect();
		
				
		clientTest1.disconnect();
		
		
		
		serverTest.sendError("Miguel", "error test");
		
		
		
		serverTest.shutdown();
	
	
		
	
		
		
		
		
	
		
		
		
		
		

	}
	
	
	
	
}
